package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @program: itcast_health
 * @create: 2019-10-21 15:56
 * @author: li he
 * @description:
 */
@RestController
@RequestMapping("/setMeal")
public class SetMealController {


    @Reference
    private SetMealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("upload")
    @PreAuthorize("hasAnyAuthority('SETMEAL_ADD')")
    public Result upload(MultipartFile imgFile) {

        try {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String filename = imgFile.getOriginalFilename();
            String suffixName = filename.substring(filename.lastIndexOf("."));

            filename = uuid + suffixName;
            //把数据上传到七牛云
            QiniuUtil.upload2Qiniu(imgFile.getBytes(), filename);
            //记录上传至七牛云的图片名称到redis缓存数据库中
            jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_RESOURCES, filename);

            return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, filename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("add")
    @PreAuthorize("hasAnyAuthority('SETMEAL_ADD')")
    public Result add(Integer[] checkGroupIds, @RequestBody Setmeal setmeal) {

        System.out.println("setmeal = " + setmeal);
        try {
            setmealService.add(checkGroupIds, setmeal);

            //把存到本地数据库中的图片名称存到reids缓存数据库中
            if (setmeal.getImg() != null && setmeal.getImg().length() > 0) {
                jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
            }

            //清空redis中列表数据的缓存
            jedisPool.getResource().del(RedisConst.SETMEAL_LIST_DB_DATA);

            return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("findPage")
    @PreAuthorize("hasAnyAuthority('SETMEAL_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.findPage(queryPageBean);
    }

    @RequestMapping("delById")
    @PreAuthorize("hasAnyAuthority('SETMEAL_DELETE')")
    public Result delById(int id) {
        try {
            setmealService.delById(id);

            //清空redis中列表数据的缓存
            jedisPool.getResource().del(RedisConst.SETMEAL_LIST_DB_DATA);

            return new Result(true,MessageConst.DELETE_SETMEAL_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            return new Result(false,MessageConst.DELETE_SETMEAL_FAIL);
        }
    }
    @RequestMapping("findById")
    @PreAuthorize("hasAnyAuthority('SETMEAL_QUERY')")
    public Result findById(int id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("findCheckGroupIds")
    @PreAuthorize("hasAnyAuthority('SETMEAL_QUERY')")
    public Result findCheckGroupIds(int id){
        try {
            List<Integer> checkGroupIds = setmealService.findCheckGroupIds(id);
            return new Result(true,null,checkGroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null);
        }
    }

    @RequestMapping("edit")
    @PreAuthorize("hasAnyAuthority('SETMEAL_EDIT')")
    public Result edit(Integer[] checkGroupIds, @RequestBody Setmeal setmeal) {

        try {
            setmealService.edit(checkGroupIds, setmeal);

            //把存到本地数据库中的图片名称存到reids缓存数据库中
            if (setmeal.getImg() != null && setmeal.getImg().length() > 0) {
                jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
            }


            //清空redis中列表数据的缓存
            jedisPool.getResource().del(RedisConst.SETMEAL_LIST_DB_DATA);

            return new Result(true, MessageConst.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.EDIT_SETMEAL_FAIL);
        }
    }
}